# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=headers
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgdesc="MinGW-w64 headers for Windows"
pkgver=10.0.0.r202.g4359b3570
pkgrel=1
_commit='4359b3570a717d20dcb4251f5b5e94459e1a64ac'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-tools" "${MINGW_PACKAGE_PREFIX}-autotools")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-Allow-to-use-bessel-and-complex-functions-without-un.patch)
sha256sums=('SKIP'
            'd641257f7e1469aff89adc33e57702b75fe9667ca035978f890eae1020b6814c')

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd ${srcdir}/mingw-w64

  git apply "${srcdir}/0001-Allow-to-use-bessel-and-complex-functions-without-un.patch"

  cd ${srcdir}/mingw-w64/mingw-w64-headers
  touch include/windows.*.h include/wincrypt.h include/prsht.h
}

build() {
  local _default_win32_winnt
  if [[ ${MSYSTEM} == MINGW* ]]; then # MINGW32 & MINGW64
      _default_win32_winnt=0x601 # Windows 7
  elif [[ ${MSYSTEM} != CLANGARM64 ]]; then # UCR64 & CLANG64 & CLANG32
      _default_win32_winnt=0x603 # Windows 8.1
  else # CLANGARM64
      _default_win32_winnt=0xA00 # Windows 10
  fi

  local _default_msvcrt=msvcrt
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _default_msvcrt=ucrt
  fi

  [[ -d ${srcdir}/headers-${MSYSTEM} ]] && rm -rf ${srcdir}/headers-${MSYSTEM}
  mkdir -p ${srcdir}/headers-${MSYSTEM} && cd ${srcdir}/headers-${MSYSTEM}

  ${srcdir}/mingw-w64/mingw-w64-headers/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-sdk=all \
    --with-default-win32-winnt=${_default_win32_winnt} \
    --with-default-msvcrt=${_default_msvcrt} \
    --enable-idl \
    --without-widl
}

package() {
  cd ${srcdir}/headers-${MSYSTEM}
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/include/pthread_signal.h
  rm ${pkgdir}${MINGW_PREFIX}/include/pthread_time.h
  rm ${pkgdir}${MINGW_PREFIX}/include/pthread_unistd.h

  install -Dm644 ${srcdir}/mingw-w64/mingw-w64-headers/ddk/readme.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ddk-readme.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
